package com.sanjiahantang.youjiaclient.module.match.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.module.common.holders.CommonHolder;
import com.sanjiahantang.youjiaclient.module.match.beans.MatchBean;
import com.sanjiahantang.youjiaclient.module.match.views.MatchView;

import java.util.ArrayList;
import java.util.HashMap;

import static com.sanjiahantang.youjiaclient.config.StaticValue.HOLDER_TYPE_GOODS_WEBVIEW;
import static com.sanjiahantang.youjiaclient.config.StaticValue.HOLDER_TYPE_MATCH_HEADER;
import static com.sanjiahantang.youjiaclient.config.StaticValue.HOLDER_TYPE_MATCH_RECOMMEND;

/**
 * Created by zy on 2017/10/18.
 */

public class MatchAdapter extends RecyclerView.Adapter {

    private Context context;
    private LayoutInflater inflater;
    private HashMap<Integer,Integer> typeList;
    private ArrayList<Object> dataList;
    public MatchAdapter(Context context, MatchBean.Data data){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.typeList = new HashMap<>();
        dataList = new ArrayList<>();
        typeList.put(0,MatchView.DETAIL_TYPE_HEADER);
        dataList.add(data);
        for (int i = 0,max = data.getTogetherList().size();i<max;i++){
            typeList.put(2 * i + 1,MatchView.DETAIL_TYPE_WEB);
            dataList.add(data.getTogetherList().get(i).getContent());
            typeList.put(2 * i + 2,MatchView.DETAIL_TYPE_GOODS);
            dataList.add(data.getTogetherList().get(i).getGoodsList());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (typeList.get(viewType)){
            case MatchView.DETAIL_TYPE_HEADER:
                return new CommonHolder((Activity) context,inflater.inflate(R.layout.item_match_detail_header,null),HOLDER_TYPE_MATCH_HEADER,((MatchBean.Data)dataList.get(viewType)).getUrl());
            case MatchView.DETAIL_TYPE_WEB:
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                WebView mView = new WebView(context);
                mView.setLayoutParams(lp);
                return new CommonHolder((Activity) context,mView, HOLDER_TYPE_GOODS_WEBVIEW, dataList.get(viewType));
            case MatchView.DETAIL_TYPE_GOODS:
                return new CommonHolder((Activity) context,inflater.inflate(R.layout.item_holder_goods_recommend,null), HOLDER_TYPE_MATCH_RECOMMEND,dataList.get(viewType));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class MatchHolder extends RecyclerView.ViewHolder{

        public MatchHolder(Activity activity, View itemView, int viewType, Object data) {
            super(itemView);

        }
    }
}
